<template>
  <el-form
    ref="userForm"
    :model="userInfo"
    label-width="120px"
    style="margin-left: 120px; margin-top: 30px"
    :rules="rules"
  >
    <el-form-item label="姓名:" prop="username">
      <el-input v-model="userInfo.username" style="width: 300px" />
    </el-form-item>
    <el-form-item label="密码:">
      <el-input
        v-model="userInfo.password2"
        type="password"
        style="width: 300px"
      />
    </el-form-item>

    <el-form-item>
      <el-button type="primary" @click="updateUser">更新</el-button>
    </el-form-item>
  </el-form>
</template>
<script>
import { getUserDetailById } from '@/api/user'
import { saveUserDetailById } from '@/api/employees'
export default {
  data() {
    return {
      userInfo: {},
      rules: {
        username: [
          { required: true, message: '用户名不能为空', triggler: 'blur' }
        ]
      }
    }
  },
  created() {
    this.loadUserInfo()
  },
  methods: {
    async loadUserInfo() {
      const { data } = await getUserDetailById(this.$route.query.id)
      this.userInfo = data
    },
    // 更新用户信息
    async updateUser() {
      await this.$refs.userForm.validate()
      await saveUserDetailById({
        ...this.userInfo,
        password: this.userInfo.password2 || ''
      })
      this.$message.success('更新成功')
    }
  }
}
</script>
